package cn.maihe.elg.operation.config;

import cn.maihe.elg.operation.utils.OkHttpClientUtil;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import lombok.Getter;
import lombok.Setter;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author WJH
 * @Date 2021/04/19
 */
@Configuration
@Setter
@Getter
public class ElgConfig {

    /**
     * 前端首页地址
     */
    @Value("${homeBaseUrl}")
    private String homeBaseUrl;

    /**
     * api入口地址
     */
    @Value("${cn.maihe.elg.operation.api-base-url}")
    private String apiBaseUrl;

    @Value("${cn.maihe.elg.operation.before-hours:24}")
    private Long beforeHours;

    @Bean
    public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyManagementException {
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
        // 加入自定义信息转换
        restTemplate.getMessageConverters().add(new MyMappingJackson2HttpMessageConverter());
        restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

        return restTemplate;
    }

    @Value("${cn.maihe.elg.operation.enable-center-id}")
    private Long enableCenterId;

    private ClientHttpRequestFactory clientHttpRequestFactory() throws NoSuchAlgorithmException, KeyManagementException {
        OkHttpClient client = OkHttpClientUtil.createIgnoreVerifySSLOkHttpClient();
//        client.newBuilder().connectionPool(new ConnectionPool(5, 300 * 1000, TimeUnit.MILLISECONDS));
//        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
//                .connectionPool(new ConnectionPool(5, 300 * 1000, TimeUnit.MILLISECONDS));
//        OkHttpClient client = clientBuilder.build();
        final OkHttp3ClientHttpRequestFactory rf = new OkHttp3ClientHttpRequestFactory(client);
        rf.setWriteTimeout(45 * 1000);
        rf.setConnectTimeout(30 * 1000);
        rf.setReadTimeout(45 * 1000);
        return rf;
    }

    /**
     * 使RestTemplate支持转换类型为text/plain的数据
     */
    public static class MyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        public MyMappingJackson2HttpMessageConverter(){
            List<MediaType> mediaTypes = new ArrayList<>();
            // 加入text/plain类型的支持
            mediaTypes.add(MediaType.TEXT_PLAIN);
            mediaTypes.add(MediaType.TEXT_HTML);
            // 如果还有其他类型的需要装换，可以一一加上
            setSupportedMediaTypes(mediaTypes);
        }
    }

    @Bean
    @ConditionalOnProperty(prefix = "logging.level",name="com.baomidou.mybatisplus.plugins.PerformanceInterceptor",havingValue = "debug")
    public PerformanceInterceptor performanceInterceptor() {
        //启用性能分析插件
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setWriteInLog(true);
        return performanceInterceptor;
    }


}
